package cn.hiapi.shopkeeper.task;

import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityHomeAd;
import cn.hiapi.shopkeeper.service.activity.ActivityHomeAdService;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.shopkeeper.task.abs.AbsTaskService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author AdinZ
 * @date 2023/2/5 1:28
 */
@Component
public class ActivityHomeAdTaskService extends AbsTaskService {

    public ActivityHomeAdTaskService(DispatchContext dispatchContext) {
        super(dispatchContext);
    }


    @Scheduled(cron = "0/3 * * * * ?")@Transactional
    public void activityStart() {
        // 开始时间  <= 当前时间
        List<ActivityHomeAd> list = this.dispatchContext.getServiceOne(ActivityHomeAdService.class).findList(new QueryWrapper()
                .eq("status", 0).le("startTime", System.currentTimeMillis()));
        this.dispatchContext.getServiceOne(ActivityHomeAdService.class).activityStart(list);
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void activityEnd() {
        //结束时间 <=
        List<ActivityHomeAd> list = this.dispatchContext.getServiceOne(ActivityHomeAdService.class).findList(new QueryWrapper()
                .eq("status", 1).le("endTime", System.currentTimeMillis()));
        this.dispatchContext.getServiceOne(ActivityHomeAdService.class).activityEnd(list);
    }


}
